package com.unicom.projt.reg;

import android.content.Intent;
import com.unicom.projt.Home;
import com.unicom.projt.Main;
import com.unicom.projt.http.AbstractHttpController;
import com.unicom.projt.http.UriConstants;
import com.unicom.projt.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: Lee
 * Date: 11-12-13
 * Time: 上午9:5
 */
public class RegisterController extends AbstractHttpController<String> {
    private static final String CHECK_NUMBER = "CHECK_NUMBER";
    private static final String SEND = "SEND";
    private static final String SUBMIT = "SUBMIT";


    private RegisterStepOnePage mRegisterStepOneActivity;
    private RegisterStepTwoPage mRegisterStepTwoActivity;
    private int step = 1;
    private String phoneNumber;
    private String password;
    private String actionType;


    public RegisterController(RegisterStepOnePage stepOnePage) {
        super(String.class);
        this.mRegisterStepOneActivity = stepOnePage;
        step = 1;
    }

    public RegisterController(RegisterStepTwoPage stepTwoPage) {
        super(String.class);
        this.mRegisterStepTwoActivity = stepTwoPage;
        step = 2;
    }

    public void sendVerificationCode(String phoneNumber, String password) {
        this.phoneNumber = phoneNumber;
        this.password = password;
        this.actionType =SEND;
        String sendNumberXml = new PostXmlBuilder().setBiPCode(RequestCode.SEND_SMS_VERIFY_CODE).setTelNo(phoneNumber).createXml();
        this.sendRequest(UriConstants.URL, sendNumberXml);
    }

    public void sendVerifyCode(String phoneNumber) {
        this.actionType = SEND;
        String sendNumberXml = new PostXmlBuilder().setBiPCode(RequestCode.SEND_SMS_VERIFY_CODE).setTelNo(phoneNumber).createXml();
        this.sendRequest(UriConstants.URL, sendNumberXml);
    }


    public void submitRegisterInfo(String phoneNumber, String password, String code) {
        this.actionType = SUBMIT;
        String svcCont = "<SvcCont><TelNo>" + phoneNumber + "</TelNo><UserPas>" + password + "</UserPas>" + "<VerificationCode>" + code + "</VerificationCode></SvcCont>";
        String postXml = new PostXmlBuilder().setBiPCode(RequestCode.REGISTERED_USER).setTelNo(phoneNumber).setSvcCont(svcCont).createXml();
        this.sendRequest(UriConstants.URL, postXml);
    }


    @Override
    public void onRequestFinish(String response) {
        if(response==null || "".equals(response) ){
            if (step == 1) {
                mRegisterStepOneActivity.hideProgressDialog();
                ToastUtils.showMessage(mRegisterStepOneActivity,"错误，请稍候再试");
            } else if (step == 2) {
                mRegisterStepTwoActivity.hideProgressDialog();
                ToastUtils.showMessage(mRegisterStepTwoActivity,"错误，请稍候再试");
            }
            return;
        }
        String respCode = SimpleXmlParser.getResponseCode(response);
         if (SEND.equals(actionType)) {
                mRegisterStepOneActivity.hideProgressDialog();
                if (ResponseCode.SUCCESS.equals(respCode)) {
                    ToastUtils.showMessage(mRegisterStepOneActivity, "验证码已经发送.");
                    Intent intent = new Intent(mRegisterStepOneActivity, RegisterStepTwoPage.class);
                    intent.putExtra("phoneNumber", this.phoneNumber);
                    intent.putExtra("password", this.password);
                    mRegisterStepOneActivity.startActivity(intent);
                    mRegisterStepOneActivity.finish();
                } else {
                    ToastUtils.showMessage(mRegisterStepOneActivity, "验证码发送失败.");
                }
        } else {
            mRegisterStepTwoActivity.hideProgressDialog();
            if (SUBMIT.equals(actionType)) {
                if (ResponseCode.SUCCESS.equals(respCode)) {
                    ToastUtils.showMessage(mRegisterStepTwoActivity, "注册成功.");
                    PageJumpUtils.jump(mRegisterStepTwoActivity, Home.class);
                    mRegisterStepTwoActivity.finish();
                }else{
                    ToastUtils.showMessage(mRegisterStepTwoActivity, "注册失败."+SimpleXmlParser.getResponseDes(response));
                }
            }
        }

    }

    @Override
    public void onNetworkAbort() {
        if (step == 1) {
            ToastUtils.showMessage(mRegisterStepOneActivity, "错误，网络连接被中断.");
            mRegisterStepOneActivity.hideProgressDialog();
        } else {
            ToastUtils.showMessage(mRegisterStepTwoActivity, "错误，网络连接被中断.");
            mRegisterStepTwoActivity.hideProgressDialog();
        }
    }

    @Override
    public void onNetworkError() {
        if (step == 1) {
            ToastUtils.showMessage(mRegisterStepOneActivity, "错误，网络连接错误.");
            mRegisterStepOneActivity.hideProgressDialog();
        } else {
            ToastUtils.showMessage(mRegisterStepTwoActivity, "错误，网络连接错误.");
            mRegisterStepTwoActivity.hideProgressDialog();
        }
    }

    @Override
    public void onResponseError() {
        if (step == 1) {
            ToastUtils.showMessage(mRegisterStepOneActivity, "错误，返回结果错误.");
            mRegisterStepOneActivity.hideProgressDialog();
        } else {
            ToastUtils.showMessage(mRegisterStepTwoActivity, "错误，返回结果错误.");
            mRegisterStepTwoActivity.hideProgressDialog();
        }
    }
}
